package com.ruoyi.web.controller;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.VideoRelationvideo;
import com.ruoyi.system.service.IVideoRelationvideoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 视频关系管理Controller
 *
 * @author ruoyi
 * @date 2022-07-01
 */
@Api("用户管理")
@Controller
@RequestMapping("/system/relationvideo")
public class VideoRelationvideoController extends BaseController {
    private String prefix = "system/relationvideo";

    @Autowired
    private IVideoRelationvideoService videoRelationvideoService;

    //    @RequiresPermissions("system:relationvideo:view")
    @GetMapping()
    public String relationvideo() {
        return prefix + "/relationvideo";
    }

    /**
     * 查询视频关系管理列表
     */
//    @RequiresPermissions("system:relationvideo:list")
    @ApiOperation("查询视频关系管理列表")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VideoRelationvideo videoRelationvideo) {
        startPage();
        List<VideoRelationvideo> list = videoRelationvideoService.selectVideoRelationvideoList(videoRelationvideo);
        return getDataTable(list);
    }

    /**
     * 导出视频关系管理列表
     */
//    @RequiresPermissions("system:relationvideo:export")
    @Log(title = "视频关系管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VideoRelationvideo videoRelationvideo) {
        List<VideoRelationvideo> list = videoRelationvideoService.selectVideoRelationvideoList(videoRelationvideo);
        ExcelUtil<VideoRelationvideo> util = new ExcelUtil<VideoRelationvideo>(VideoRelationvideo.class);
        return util.exportExcel(list, "视频关系管理数据");
    }

    /**
     * 新增视频关系管理
     */

    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存视频关系管理
     */
    @ApiOperation("新增保存视频关系管理")
//    @RequiresPermissions("system:relationvideo:add")
    @Log(title = "视频关系管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(VideoRelationvideo videoRelationvideo) {
        return toAjax(videoRelationvideoService.insertVideoRelationvideo(videoRelationvideo));
    }

    /**
     * 修改视频关系管理
     */
//    @RequiresPermissions("system:relationvideo:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        VideoRelationvideo videoRelationvideo = videoRelationvideoService.selectVideoRelationvideoById(id);
        mmap.put("videoRelationvideo", videoRelationvideo);
        return prefix + "/edit";
    }

    /**
     * 修改保存视频关系管理
     */
//    @RequiresPermissions("system:relationvideo:edit")
    @ApiOperation("修改保存视频关系管理")
    @Log(title = "视频关系管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(VideoRelationvideo videoRelationvideo) {
        return toAjax(videoRelationvideoService.updateVideoRelationvideo(videoRelationvideo));
    }

    /**
     * 删除视频关系管理
     */
//    @RequiresPermissions("system:relationvideo:remove")
    @ApiOperation("删除视频关系管理")
    @Log(title = "视频关系管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(videoRelationvideoService.deleteVideoRelationvideoByIds(ids));
    }

    /**
     * 添加(修改)视频关注
     *
     * @param videoRelationvideo
     * @return
     */
    @PostMapping("/myadd")
    @ResponseBody
    public AjaxResult myadd(VideoRelationvideo videoRelationvideo) {
        Long IndexId = 0l;
        VideoRelationvideo item = new VideoRelationvideo();
        item.setUserId(videoRelationvideo.getUserId());
        item.setVideoId(videoRelationvideo.getVideoId());
        List<VideoRelationvideo> videoRelationvideos = videoRelationvideoService.selectVideoRelationvideoList(item);
        if (videoRelationvideos.size() == 0) {
            return toAjax(videoRelationvideoService.insertVideoRelationvideo(videoRelationvideo));
        } else {
            IndexId = videoRelationvideos.get(0).getId();
            videoRelationvideo.setId(IndexId);
            if (videoRelationvideo.getUpStatus() == 0 && videoRelationvideo.getCollectStatus() == 0) {
                return toAjax(videoRelationvideoService.deleteVideoRelationvideoById(IndexId));
            } else {
                return toAjax(videoRelationvideoService.updateVideoRelationvideo(videoRelationvideo));
            }
        }
    }

    @PostMapping("/mylist")
    @ResponseBody
    public AjaxResult getmyList(VideoRelationvideo videoRelationvideo) {
        return success(videoRelationvideoService.getupStatusAndcollectStatusList(videoRelationvideo));
    }


}
